package com.lylg.picture_share.controller;

import com.github.pagehelper.PageInfo;
import com.lylg.picture_share.entity.Collect;
import com.lylg.picture_share.entity.Image;
import com.lylg.picture_share.entity.User;

import com.lylg.picture_share.service.CollectService;
import com.lylg.picture_share.service.ImageService;
import com.lylg.picture_share.service.ImageTypeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class FrontIndexController {
    private PageInfo<Image> pageInfo;
    @Autowired
    private ImageTypeService imageTypeService;
    @Autowired
    private ImageService imageService;

    @Autowired
    private CollectService collectService;
    @RequestMapping("/front/index")
    public String goIndex(Map<String,Object> model, Image queryBean, Integer currentPage){
        //因为页面首次加载的时候传递不过来页码信息
        if(currentPage==null){

            currentPage=1;
        }
        else{

            if(pageInfo!=null) {
                if (currentPage > pageInfo.getPages()) {
                    currentPage = pageInfo.getPages();
                }
                //防止点击上一页越界
                if (currentPage == 0) {
                    currentPage = 1;
                }
            }
        }

        Integer pageSize=4;


        pageInfo=imageService.getAllImagesFront(queryBean,currentPage,pageSize);
        model.put("pb",pageInfo);
        model.put("qb",queryBean);
        //数据模型中放了类型表中所有的类型信息
        model.put("types",imageTypeService.selectAllImageType());
        //如果想展示热门视频，调用services层的方法获得热门视频，放到数据模型中
        return "/front/index.html";//请求转发到首页
    }


    @RequestMapping("/front/imagedetailui")
    public String imageDetaiui(Map<String,Object> model, Integer image_id, HttpSession session){
            Image image = imageService.getImageById(image_id);
            model.put("image",image);
            //当前视频有没有被登录的用户进行收藏
             Integer userId=0;
             if(session.getAttribute("loginuser")!=null){
                 userId=((User)session.getAttribute("loginuser")).getUser_id();
             }
             Collect c=new Collect(userId,image_id);
             //判断有没有收藏该视频
             if(collectService.getCollectImageByUser(c)!=null){
                 model.put("isCollect",true);
             }
             else{
                 model.put("isCollect",false);
             }

            return "/front/imagedetail.html";
    }




    @RequestMapping("/front/addPlayCount")
    @ResponseBody
    public String doAddPlayCount(Integer image_id){

        imageService.addPlayCount(image_id);

        return "success";
    }
    @RequestMapping("/front/registerUI")
    public String toRegister(){
        return "/front/register.html";
    }
}
